<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class ToolController extends BaseController
{
	/**
	 * 图片上传
	 * @param Request $request
	 * @return json
	 */
    public function ajax_img(Request $request)
	{
		if ($request->isMethod('POST')) { //判断文件是否是 POST的方式上传
			$tmp = $request->file('file');
			$type=$request->input('img_type');
			$size=$request->input('size');
			if($size > 1048576){
				return $this->resultJson( "上传文件不能大于1MB",1 );
			}
			$path = '/'.$type; //public下的type
			if ($tmp->isValid()) { //判断文件上传是否有效
				$FileType = $tmp->getClientOriginalExtension(); //获取文件后缀
				$FilePath = $tmp->getRealPath(); //获取文件临时存放位置
				$FileName = $path . '/'.date('Y-m-d') . uniqid() . '.' . $FileType; //定义文件名
				Storage::disk('classify')->put($FileName, file_get_contents($FilePath)); //存储文件
//				$path=$path . '/' . $FileName; //文件路径
				return $this->resultJson($FileName ,0 );
			}
			return $this->resultJson( "文件上传无效",1 );
		}
	}

}
